home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Typography Samples / TrackingLayoutSample ƒ / TrackingLayoutSample.c next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  6.9 KB  |  303 lines  |  [TEXT/MPS ]

  1. /*
  2.     Tracking Layout Sample Program
  3.     By Eric Mader,
  4.     After Mike Fairman, Oliver Steele et. al.
  5. */
  6.  
  7. /* Copyright ©1989, 1990, 1991 Apple Computer, Inc.  All rights reserved. */
  8.  
  9.  
  10. #ifndef THINK_C
  11. #include <Quickdraw.h>
  12. #include <Fonts.h>
  13. #include <Windows.h>
  14. #include <Dialogs.h>
  15. #include <Events.h>
  16. #include <Memory.h>
  17. #include <Menus.h>
  18. #include <String.h>
  19. #include <Desk.h>
  20. #include <Script.h>
  21. #include <ToolUtils.h>
  22. #define thePort qd.thePort
  23. #define screenBits qd.screenBits
  24. #endif
  25.  
  26. #include "graphics libraries.h"
  27. #include "qd library.h"
  28. #include "graphics debugging.h"
  29. #include "graphics routines.h"
  30. #include "graphics toolbox.h"
  31.  
  32. #include "layout types.h"
  33. #include "layout routines.h"
  34. #include "layout library.h"
  35.  
  36. /* This macro is useful for constructing fixed values */
  37. #define f(a,b) (((fixed) (a) << 16) + (b))
  38.  
  39. /* various menu and dialog things */
  40. enum {aboutDLOG = 128, appleMenuID = 128, fileMenuID, editMenuID}; /* resource ID's */
  41.  
  42. enum {okButton = 1, showsItem, authorItem};
  43.  
  44. enum {
  45.     /* Apple Menu */
  46.     aboutCommand = 1,
  47.     
  48.     /* File Menu */
  49.     quitCommand = 1,
  50.     
  51.     /* Edit Menu */
  52.     undoCommand = 1,
  53.     cutCommand,
  54.     copyCommand,
  55.     pasteCommand,
  56.     clearCommand};
  57.  
  58. WindowPtr myWindow, whichWindow;
  59. gxViewPort myPort;
  60. Rect qdWindowRect;
  61. Rect growRect = {40, 40, 32767, 32767};
  62. Rect bigRect = {-32768, -32768, 32767, 32767};
  63. gxRectangle windowRect;
  64.  
  65. MenuHandle appleMenu, fileMenu, editMenu;
  66. Boolean done = false;
  67.  
  68. gxColor colorWhite = xRGB (0xFFFF, 0xFFFF, 0xFFFF);    /* white */
  69. gxColor hsvInitial = xHSV (0, 0xFFFF, 0xFFFF); /* red */
  70.  
  71. static void ShowAboutBox ()
  72. {    GrafPtr savePort;
  73.     DialogPtr theDialog;
  74.     short itemType;
  75.     Handle itemHdl;
  76.     Rect itemRect;
  77.     short itemHit;
  78.  
  79.     GetPort(&savePort);
  80.     theDialog = GetNewDialog(aboutDLOG, nil, (WindowPtr) -1);
  81.     SetPort(theDialog);
  82.  
  83.     GetDItem(theDialog, authorItem, &itemType, &itemHdl, &itemRect);
  84.     SetIText(itemHdl, (StringPtr) "\pRice Dream");
  85.     GetDItem(theDialog, showsItem, &itemType, &itemHdl, &itemRect);
  86.     SetIText(itemHdl, (StringPtr) "\pTracking");
  87.  
  88.     do {
  89.         ModalDialog(nil, &itemHit);
  90.     } while (itemHit != okButton);
  91.  
  92.     CloseDialog(theDialog);
  93.  
  94.     SetPort(savePort);
  95. }
  96.  
  97. static void SetupMenus ()
  98. {
  99.     InsertMenu (appleMenu = GetMenu (appleMenuID), 0);
  100.     AddResMenu (appleMenu, (ResType) 'DRVR');
  101.     InsertMenu (fileMenu = GetMenu (fileMenuID), 0);
  102.     InsertMenu (editMenu = GetMenu (editMenuID), 0);
  103.     DrawMenuBar ();
  104. }
  105.  
  106. static void DoMenuCommand (long mResult)
  107. {    short theItem = LoWord (mResult), theMenuID = HiWord (mResult);
  108.     GrafPtr savePort;
  109.     Str255 daName;
  110.     
  111.     switch (theMenuID)
  112.     {    case appleMenuID:
  113.             if (theItem == aboutCommand) ShowAboutBox ();
  114.             else
  115.             {    GetItem (appleMenu, theItem, daName);
  116.                 GetPort (&savePort);
  117.                 (void) OpenDeskAcc (daName);
  118.                 SetPort (savePort);
  119.             }
  120.         break;
  121.             
  122.         case fileMenuID:
  123.             if (theItem == quitCommand) done = true;
  124.         break;
  125.             
  126.         case editMenuID:
  127.         break;
  128.     }
  129.     
  130.     HiliteMenu (0);
  131. }
  132.  
  133. void main()
  134. {
  135.     EventRecord theEvent;
  136.     gxShape layoutTight, layoutNormal, layoutLoose, whiteOut;
  137.     gxRunControls controls;
  138.     gxLayoutOptions gxLayoutOptions;
  139.     char *text1 =      "track track track track track";
  140.     gxPoint posn;
  141.     GDHandle max;
  142.     short mbh = GetMBarHeight ();
  143.     
  144.     MaxApplZone(); MoreMasters(); MoreMasters();
  145.  
  146.     SetGraphicsLibraryErrors();
  147.     SetGraphicsLibraryNotices ();
  148. /*    GXSetValidation(gxInternalValidation | gxStructureValidation | gxNoMemoryManagerValidation);    /* uncomment this for less speed and more error-checking */
  149.  
  150.     InitCommonColors ();
  151.     
  152.     InitGraf(&thePort);
  153.     InitFonts();
  154.     InitWindows();
  155.     InitMenus ();
  156.     InitCursor();
  157.  
  158.     SetupMenus ();
  159.     
  160.     /* find the deepest monitor, and make a window that just covers it */
  161.     max = GetMaxDevice (&bigRect);
  162.     qdWindowRect = (**max).gdRect;
  163.     
  164.     /* bring it down one mbh for the header, maybe another if on main screen */
  165.     if (qdWindowRect.top == 0 && qdWindowRect.left == 0) qdWindowRect.top += mbh;
  166.     qdWindowRect.top += mbh;
  167.         
  168.     InsetRect (&qdWindowRect, 4, 4);
  169.     ShortRectToFixed (&qdWindowRect, &windowRect);
  170.     
  171.     myWindow = NewWindow(nil, &qdWindowRect, (StringPtr) "\pTracking Layout Sample",
  172.              true, documentProc, (WindowPtr) -1L, true, 0L);
  173.     
  174.     myPort = GXNewWindowViewPort (myWindow);
  175.     SetDefaultViewPort (myPort);
  176.  
  177.     /*
  178.          When we want to erase the whole window, we just GXDrawShape (whiteOut).
  179.      */
  180.     whiteOut = GXNewShape(gxFullType);
  181.     GXSetShapeColor (whiteOut, &colorWhite);
  182.  
  183.     /* Make a default gxLayoutOptions and gxRunControls */
  184.     InitializeLayoutOptions (&gxLayoutOptions);
  185.     InitializeRunControls (&controls);
  186.     
  187.     /*
  188.         Position the layout half way down the left edge of the window. Set 
  189.         the layout's width to the window's width and set the flushness to 1/2, this
  190.         will cause the layout to center in the window.
  191.     */
  192.     gxLayoutOptions.width = windowRect.right - windowRect.left;
  193.     gxLayoutOptions.flush = fract1/2;
  194.     posn.x = 0;
  195.     posn.y = (windowRect.bottom - windowRect.top) / 2;
  196.     
  197.     /* Build and draw the layouts. */
  198.     
  199.     layoutNormal = NewSingleLayout (
  200.         text1,
  201.         (char *) "\pTimes Roman", 
  202.         ff(32),
  203.         &gxLayoutOptions,
  204.         &posn,
  205.         0,
  206.         &controls,
  207.         nil,
  208.         0,
  209.         nil);
  210.     
  211.     posn.y -= ff(40);
  212.     controls.track = -ff(2);
  213.     layoutTight = NewSingleLayout (
  214.         text1,
  215.         (char *) "\pTimes Roman", 
  216.         ff(32),
  217.         &gxLayoutOptions,
  218.         &posn,
  219.         0,
  220.         &controls,
  221.         nil,
  222.         0,
  223.         nil);
  224.     
  225.     posn.y += ff(80);
  226.     controls.track = ff(2);
  227.     layoutLoose = NewSingleLayout (
  228.         text1,
  229.         (char *) "\pTimes Roman", 
  230.         ff(32),
  231.         &gxLayoutOptions,
  232.         &posn,
  233.         0,
  234.         &controls,
  235.         nil,
  236.         0,
  237.         nil);
  238.     
  239.     GXDrawShape (layoutTight); GXDrawShape (layoutNormal); GXDrawShape (layoutLoose);
  240.  
  241.     /* Now just spin in a simple event loop until it's time to go */
  242.     while (!done)
  243.     {
  244.         if (WaitNextEvent(everyEvent, &theEvent, 0, nil))
  245.         {
  246.             switch(theEvent.what)
  247.             {
  248.                 case mouseDown:
  249.                 switch (FindWindow(theEvent.where, &whichWindow))
  250.                 {    case inSysWindow:
  251.                         SystemClick(&theEvent, whichWindow);
  252.                         break;
  253.                     
  254.                     case inMenuBar:
  255.                         DoMenuCommand (MenuSelect (theEvent.where));
  256.                     break;
  257.                         
  258.                     case inDrag:
  259.                         DragWindow(whichWindow, theEvent.where, &screenBits.bounds);
  260.                     break;
  261.  
  262.                     case inGrow:
  263.                     {    register long newSize;
  264.  
  265.                         newSize = GrowWindow(whichWindow, theEvent.where, &growRect);
  266.                         SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), true);
  267.                     }
  268.                     break;
  269.                     
  270.                     case inGoAway:
  271.                         if (TrackGoAway(whichWindow, theEvent.where)) done = true;
  272.                     break;
  273.  
  274.                     case inContent:
  275.                         if (whichWindow != FrontWindow())
  276.                             SelectWindow(whichWindow);
  277.                     break;
  278.                 }
  279.                 break;
  280.  
  281.                 case keyDown:
  282.                 case autoKey:
  283.                     if (myWindow == FrontWindow () && theEvent.modifiers & cmdKey)
  284.                         DoMenuCommand (MenuKey (theEvent.message & charCodeMask));
  285.                 break;
  286.  
  287.                 case updateEvt:
  288.                     BeginUpdate((WindowPtr)theEvent.message);
  289.                     GXDrawShape (whiteOut);
  290.                     GXDrawShape (layoutTight); GXDrawShape (layoutNormal); GXDrawShape (layoutLoose);
  291.                     EndUpdate((WindowPtr)theEvent.message);
  292.                 break;
  293.             }
  294.         }
  295.     }
  296.     /* dispose everything we've allocated. */
  297.     GXDisposeShape(layoutTight); GXDisposeShape (layoutNormal); GXDisposeShape (layoutLoose);
  298.     DisposeWindow(myWindow);
  299.     GXDisposeShape (whiteOut);
  300.     
  301.     DisposeCommonColors ();
  302. }
  303.